/**
 * Created by jichong on 16/10/21.
 */

//布局相关的函数
function SetOffsetToNode(node, xoffset, yoffset){
  cord = node.getLocation();
  this.setLocation(cord.x+xoffset, cord.y+yoffset)
}

var JCFamily={
  Version:"0.0.1",
  scene:null,
  VerticlGap:100,
  MIN_LEVEL:1000000,
  MAX_LEVEL:-100000,
  NodeWidth:100,
  NodeHeight:60,
  SpouseGap:120,
  BrotherGap:160,
  LevelNodes:{},
  Links:{},
  Persons:[
    {id:"A1", name:"张父", sex:"男"},
    {id:"A8", name:"张妻", sex:"女"},
    {id:"A9", name:"张前妻", sex:"女"},
    {id:"A2", name:"张子", sex:"男"},
    {id:"A3", name:"张二子", sex:"男"},
    {id:"A4", name:"张子妻", sex:"女"},
    {id:"A5", name:"张爷爷", sex:"男"},
    {id:"A6", name:"张奶奶", sex:"女"},
    {id:"A11", name:"张大孙", sex:"女"},
    {id:"A12", name:"张二孙", sex:"男"},
    {id:"A21", name:"张大一孙", sex:"男"},
    {id:"A22", name:"张大二孙", sex:"女"},
    {id:"A10", name:"张二父", sex:"男"},
    {id:"A20", name:"张三父", sex:"男"}
  ],
  Edges:[
    {from:"A1", to:"A2", type:"父子"},
    {from:"A1", to:"A3", type:"父子"},
    {from:"A1", to:"A8", type:"夫妻"},
    {from:"A1", to:"A9", type:"夫妻"},
    {from:"A8", to:"A2", type:"母子"},
    {from:"A8", to:"A3", type:"母子"},
    {from:"A5", to:"A1", type:"父子"},
    {from:"A4", to:"A2", type:"夫妻"},
    {from:"A6", to:"A1", type:"母子"},
    {from:"A5", to:"A10", type:"父子"},
    {from:"A10", to:"A11", type:"父子"},
    {from:"A10", to:"A12", type:"父子"},
    {from:"A5", to:"A20", type:"父子"},
    {from:"A20", to:"A21", type:"父子"},
    {from:"A20", to:"A22", type:"父子"}
  ],
  PersonNode:function(person){
    var n = new JTopo.Node();
    n.SetOffsetToNode = SetOffsetToNode;
    n.Person = person
    n.text = person.name; // 文字
    n.textPosition = 'Middle_Center';// 文字居中
    n.textOffsetY = -8; // 文字向下偏移8个像素
    n.font = '14px 微软雅黑'; // 字体
    n.setLocation(180, 100); // 位置
    n.setSize(this.NodeWidth, this.NodeHeight);  // 尺寸
    n.borderRadius = 5; // 圆角
    n.borderWidth = 2; // 边框的宽度
    n.borderColor = '255,255,255'; //边框颜色
    if (person.sex == '女'){
      n.fillColor = '255,0,0';
    }else{
      n.fillColor = '0,0,255';
    }
    n.alpha = 1; //透明度
    n.addEventListener("click", function(event){
      var p = event.target.Person;
      drawSpouse(p);
      drawParent(p);
      drawChild(p);
      JCFamily.RefreshMap(p);
    });
    this.scene.add(n);
    return n;
  },
  SpouseLink:function(f, t){
    var link = new JTopo.CurveLink(f.node, t.node);
    link.strokeColor = '204,204,204';
    link.bundleGap = 5; // 线条之间的间隔
    link.lineWidth = 1;
    this.scene.add(link);
    var key = f.id +"|" + t.id;
    this.Links[key] = 1;
    key = t.id +"|" + f.id;
    this.Links[key] = 1;
    return link;
  },
  ParentLink:function(f, t){
    var link = new JTopo.Link(f.node, t.node);
    link.strokeColor = '204,204,204';
    link.bundleGap = 5; // 线条之间的间隔
    link.lineWidth = 1;
    this.scene.add(link);
    var key = f.id +"|" + t.id;
    this.Links[key] = 1;
    key = t.id +"|" + f.id;
    this.Links[key] = 1;
    return link;
  },
  FindSpouse:function(p){
    var ret=[];
    for (var i=0; i<this.Edges.length; i++ ){
      var r = this.Edges[i];
      if (r.type == '夫妻' && (r.from == p.id || r.to == p.id)){
        ret.push(r);
      }
    }
    return ret;
  },
  FindChild:function(p){
    var ret=[];
    for (var i=0; i<this.Edges.length; i++ ){
      var r = this.Edges[i];
      if ((r.type == '父子' || r.type == '母子')  && r.from == p.id){
        ret.push(r);
      }
    }
    return ret;
  },
  FindParent:function(p){
    var ret=[];
    for (var i=0; i<this.Edges.length; i++ ){
      var r = this.Edges[i];
      if ((r.type == '父子' || r.type == '母子') && r.to == p.id){
        ret.push(r);
      }
    }
    return ret;
  },
  FindPerson:function(pid){
    for (var i=0; i<this.Persons.length; i++ ) {
      var p = this.Persons[i];
      if (p.id == pid){
        return p;
      }
    }
    return null;
  },
  HasLink:function(f, t){
    var key = f.id +"|" + t.id;
    if (this.Links[key] == 1){
      return true;
    }
    return false;
  },
  RefreshNode:function(p){
    p.node.text = p.name+" " + p.x + "," + p.y;
  },
  sortNode:function(a, b)
  {
    return a.x - b.x
  },
  RefreshMap:function(p){ //以P为锚点,重新整理图
    var cx = p.node.x;
    var cy = p.node.y;
    var cl = p.level;


    //整理一层
    for (var l=this.MIN_LEVEL; l<=this.MAX_LEVEL; l++){
      var nodes= [];

      for (var i=0; i<this.Persons.length; i++ ) {
        var n = this.Persons[i];
        if (n.level == l){
          nodes[nodes.length] = n;
        }
      }
      nodes = nodes.sort(this.sortNode);

      for (var j=0;j<nodes.length;j++){
        var n = nodes[j];
        n.node.setLocation(j*(JCFamily.NodeWidth+20), cy + (l-cl)*JCFamily.VerticlGap)
        //JTopo.Animate.stepByStep(n.node, {x: j*(JCFamily.NodeWidth+20), y:cy + (l-cl)*JCFamily.VerticlGap}, 1000, false).start();
      }
    }

    //最后统一锚定到点击的P
    //for (i=0;i<this.Persons.length;i++){
    //  var n = this.Persons[i];
    //  n.node.setLocation(n.node.x +(cx - p.node.x), n.node.y + (cy - p.node.y));
    //}





  }

};